package com.enterprise.support.utility;

import org.apache.log4j.Logger;
import org.h2.tools.Server;

import java.sql.SQLException;


/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 11-5-17
 * Time: 下午3:45
 * To change this template use File | Settings | File Templates.
 */
public class H2ServerManager {
    private static Enterprise config = new Enterprise(Configuration.getProperties("/config/h2/config.properties"));
    public static boolean isRun = false;
    private static Logger logger = Logger.getLogger(H2ServerManager.class);

    public static void startServer() {
        try {
            logger.info("Start H2 Database .....");
            Server.main("-web", "-tcp",
                    "-tcpPort", config.getStringByDefaultValue("tcpprot", config.getStringByDefaultValue("tcpport", "9080")),
                    "-webPort", config.getStringByDefaultValue("webprot", config.getStringByDefaultValue("tcpport", "8082"))
            );
            isRun = true;
        } catch (SQLException ex) {
            logger.error("Start H2 Database Error", ex);
        }
    }

    public static void stopServer() {
        try {
            if (isRun) {
                logger.info("Stop H2 Database .....");
                //Server.main("-tcpShutdown");
            }
        } catch (Exception ex) {
            logger.error("Stop H2 Database  Error", ex);
        }
    }

    public static void main(String[] args) {
        H2ServerManager server = new H2ServerManager();
        server.startServer();
        /*  AbstractDaoImpl logic = SpringLocator.getApplicationContext().getBean(AbstractDaoImpl.class);
        for (int i = 0; i < 100011; i++) {
            AdminEntity entity = new AdminEntity();
            entity.setCreate(new Date());
            entity.setUsername("username_" + i);
            entity.setPassword("password_" + i);
            logic.save(entity);
        }*/
    }
}
